<template>
  <el-dialog title="表格通讯录" :visible.sync="open" width="1000px">
    <el-table :data="tableData" :header-cell-style="getRowClass" height="450px">
      <el-table-column prop="employeeNumber" label="工号"></el-table-column>
      <el-table-column prop="userName" label="姓名"></el-table-column>
      <el-table-column prop="englishName" label="英文名"></el-table-column>
      <el-table-column prop="deptName" label="部门"></el-table-column>
      <el-table-column prop="postName" label="职位" width="150"></el-table-column>
      <el-table-column prop="entryTime" label="入职时间" width="100">
        <template slot-scope="scope">{{scope.row.entryTime | filtData}}</template>
      </el-table-column>
      <el-table-column prop="formalTime" label="转正时间" width="100">
        <template slot-scope="scope">{{scope.row.formalTime | filtData}}</template>
      </el-table-column>
      <el-table-column prop="sex" label="性别">
        <template slot-scope="scope">{{scope.row.sex == 0?'男':'女'}}</template>
      </el-table-column>
      <el-table-column prop="maritalStatus" label="婚姻状况">
        <template slot-scope="scope">{{scope.row.maritalStatus == 0?'未':'已'}}</template>
      </el-table-column>
      <el-table-column prop="identityNumber" label="身份证号" width="180"></el-table-column>
      <el-table-column prop="identityValidity" label="身份证有效期" width="120">
        <template slot-scope="scope">{{scope.row.identityValidity | filtData}}</template>
      </el-table-column>
      <el-table-column prop="birthDate" label="出生日期" width="100">
        <template slot-scope="scope">{{scope.row.birthDate | filtData}}</template>
      </el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="politicCountenance" label="政治面貌"></el-table-column>
      <el-table-column prop="nation" label="民族"></el-table-column>
      <el-table-column prop="householdRegister" label="户籍"></el-table-column>
      <el-table-column prop="nativePlace" label="籍贯"></el-table-column>
      <el-table-column prop="permanentResidence" label="户口所在地址" width="120"></el-table-column>
      <el-table-column prop="currentAddress" label="现居地址"></el-table-column>
      <el-table-column prop="phoneNumber" label="联系方式"></el-table-column>
      <el-table-column prop="contractStartDate" label="合同开始日期" width="120"></el-table-column>
      <el-table-column prop="contractEndDate" label="合同结束日期" width="120"></el-table-column>
      <el-table-column prop="graduateSchool" label="毕业学校"></el-table-column>
      <el-table-column prop="graduateTime" label="毕业时间" width="100"></el-table-column>
      <el-table-column prop="degree" label="学历"></el-table-column>
      <el-table-column prop="major" label="专业"></el-table-column>
      <el-table-column prop="qualification" label="职业资格"></el-table-column>
      <el-table-column prop="fundNumber" label="个人公积金账号" width="120"></el-table-column>
      <el-table-column prop="socialSecurityNumber" label="社保账号"></el-table-column>
      <el-table-column prop="emergencyContact" label="紧急联系人姓名" width="120"></el-table-column>
      <el-table-column prop="emergencyContactPhone" label="紧急联系人号码" width="120"></el-table-column>
      <el-table-column prop="emergencyContactAddress" label="紧急联系人地址" width="120"></el-table-column>
      <el-table-column
        prop="leaveDate"
        label="离职日期"
        width="120"
        v-if="jobStatus === '2'"
      ></el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        @click="submitForm('formData')"
        size="small"
        :disabled="loading"
        :icon="loading?'el-icon-loading':'el-icon-circle-plus-outline'"
      >保存</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { saveUserInfo } from "@/api/addressBook";
import { getDeptList, listPost } from "@/api/system";
import { formatDate } from "assets/js/date";
export default {
  data() {
    return {
      open: false,
      tableData: [],
      DeptList: [],
      postAll: [],
      loading: false,
      jobStatus: '1'
    };
  },
  created() {
    this.load();
  },
  filters: {
    filtData(time) {
      if (time) {
        let date = new Date(time);
        return formatDate(date, "yyyy-MM-dd");
      }
    }
  },
  methods: {
    show(arr) {
      const tableData = [];
      arr.forEach((item, index) => {
        tableData.push({
          employeeNumber: item.编号,
          userName: item.姓名,
          englishName: item.英文名,
          surname: item.姓,
          name: item.名,
          deptName: item.部门,
          postName: item.职位,
          deptId: this.filterDept(item.部门),
          postIds: this.filterPost(item.职位),
          entryTime: this.filterDate(item.入职时间),
          formalTime: this.filterDate(item.转正时间),
          sex: item.性别 === "男" ? 0 : 1,
          maritalStatus: item.婚姻状况 === "未" ? 0 : 1,
          identityNumber: item.身份证号,
          identityValidity: this.filterDate(item.身份证有效期),
          birthDate: this.filterDate(item.出生日期),
          age: this.GetAge(
            formatDate(new Date(this.filterDate(item.出生日期)), "yyyy-MM-dd")
          ),
          politicCountenance: item.政治面貌,
          nation: item.民族,
          householdRegister: item.户籍,
          nativePlace: item.籍贯,
          permanentResidence: item.户口所在地址,
          currentAddress: item.现居地址,
          phoneNumber: item.联系方式,
          contractStartDate: !isNaN(item.合同开始日期)
            ? formatDate(
                new Date(this.filterDate(item.合同开始日期)),
                "yyyy-MM-dd"
              ) + ""
            : item.合同开始日期,
          contractEndDate: !isNaN(item.合同结束日期)
            ? formatDate(
                new Date(this.filterDate(item.合同结束日期)),
                "yyyy-MM-dd"
              ) + ""
            : item.合同结束日期,
          graduateSchool: item.毕业学校,
          graduateTime: item.毕业时间
            ? item.毕业时间.toString().indexOf(".") == -1
              ? formatDate(
                  new Date(this.filterDate(item.毕业时间)),
                  "yyyy-MM-dd"
                ) + ""
              : item.毕业时间 + ""
            : "",
          degree: item.学历,
          major: item.专业,
          qualification: item.职业资格,
          fundNumber: item.个人公积金账号,
          socialSecurityNumber: item.社保账号,
          emergencyContact: item.紧急联系人姓名,
          emergencyContactPhone: item.紧急联系人号码,
          emergencyContactAddress: item.紧急联系人地址,
          email: item.邮箱,
          leaveDate: item.离职日期
            ? item.离职日期.toString().indexOf(".") == -1
              ? formatDate(
                  new Date(this.filterDate(item.离职日期)),
                  "yyyy-MM-dd"
                ) + ""
              : item.离职日期 + ""
            : ""
        });
      }); 
      this.tableData = tableData;
      this.open = true;
    },
    load() {
      getDeptList().then(res => {
        this.DeptList = res.data.rows;
      });
      listPost().then(res => {
        this.postAll = res.data.rows;
      });
    },
    filterDept(name) {
      const DeptItem = this.DeptList.find(item => item.deptName === name);
      return DeptItem ? DeptItem.deptId : "";
    },
    filterPost(name) {
      const postItem = this.postAll.find(item => item.postName === name);
      if (postItem) {
        return [postItem.postId];
      } else {
        return [];
      }
    },
    filterDate(time) {
      const second = 25569,
        day_timestamp = 24 * 60 * 60 * 1000;
      if ((+time - second) * day_timestamp) {
        return (+time - second) * day_timestamp;
      } else {
        return "";
      }
    },
    // 标题背景颜色
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "background: #EBEEF5 ";
      } else {
        return "";
      }
    },
    GetAge(strBirthday) {
      var returnAge,
        strBirthdayArr = strBirthday.split("-"),
        birthYear = strBirthdayArr[0],
        birthMonth = strBirthdayArr[1],
        birthDay = strBirthdayArr[2],
        d = new Date(),
        nowYear = d.getFullYear(),
        nowMonth = d.getMonth() + 1,
        nowDay = d.getDate();
      if (nowYear == birthYear) {
        returnAge = 0; //同年 则为0周岁
      } else {
        var ageDiff = nowYear - birthYear; //年之差
        if (ageDiff > 0) {
          if (nowMonth == birthMonth) {
            var dayDiff = nowDay - birthDay; //日之差
            if (dayDiff < 0) {
              returnAge = ageDiff - 1;
            } else {
              returnAge = ageDiff;
            }
          } else {
            var monthDiff = nowMonth - birthMonth; //月之差
            if (monthDiff < 0) {
              returnAge = ageDiff - 1;
            } else {
              returnAge = ageDiff;
            }
          }
        } else {
          returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
        }
      }
      return returnAge; //返回周岁年龄
    },
    submitForm() { 
      this.loading = true;
      saveUserInfo(this.tableData).then(res => {
        this.loading = false;
        if (res.data.code === this.ERR_OK) {
          this.$message.success(res.data.msg);
          this.open = false;
          this.$emit("load");
          if (res.data.data.length) {
            this.$notify({
              title: "以下名称已经存在",
              message: res.data.data,
              type: "warning"
            });
          }
        } else {
          this.$message.error(res.data.msg);
        }
      });
    }
  }
};
</script>